package com.ala4.oxcafe.boot.converter;

import cn.idev.excel.converters.Converter;
import cn.idev.excel.enums.CellDataTypeEnum;
import cn.idev.excel.metadata.GlobalConfiguration;
import cn.idev.excel.metadata.data.ReadCellData;
import cn.idev.excel.metadata.data.WriteCellData;
import cn.idev.excel.metadata.property.ExcelContentProperty;
import com.ala4.oxcafe.boot.annotation.ExcelIntToStr;

import java.util.HashMap;
import java.util.Map;

/**
 * @author PING
 * @version 1.0.0
 * @date 2025/8/15 16:43
 */
public class ExcelIntToStrConverter implements Converter<Integer> {

    @Override
    public Class<Integer> supportJavaTypeKey() {
        return Integer.class;
    }

    @Override
    public CellDataTypeEnum supportExcelTypeKey() {
        return CellDataTypeEnum.STRING;
    }

    @Override
    public Integer convertToJavaData(ReadCellData<?> cellData, ExcelContentProperty contentProperty,
                                     GlobalConfiguration globalConfiguration) {
        // 获取字段上的注解
        ExcelIntToStr annotation = contentProperty.getField().getAnnotation(ExcelIntToStr.class);
        if (annotation == null) {
            throw new IllegalStateException("Field must be annotated with @ExcelEnumMapping");
        }

        // 解析映射关系
        Map<String, Integer> mapping = parseMapping(annotation.value());

        String cellValue = cellData.getStringValue();
        Integer result = mapping.get(cellValue);

        if (result == null) {
            throw new IllegalArgumentException("[" + contentProperty.getField().getName() +
                    "] 未知的枚举值: " + cellValue);
        }
        return result;
    }

    @Override
    public WriteCellData<?> convertToExcelData(Integer value, ExcelContentProperty contentProperty,
                                               GlobalConfiguration globalConfiguration) {
        // 获取字段上的注解
        ExcelIntToStr annotation = contentProperty.getField().getAnnotation(ExcelIntToStr.class);
        if (annotation == null) {
            throw new IllegalStateException("Field must be annotated with @ExcelEnumMapping");
        }

        // 解析反向映射
        Map<Integer, String> reverseMapping = parseReverseMapping(annotation.value());

        String displayValue = reverseMapping.get(value);
        if (displayValue == null) {
            throw new IllegalArgumentException("[" + contentProperty.getField().getName() +
                    "] 未知的状态值: " + value);
        }
        return new WriteCellData<>(displayValue);
    }

    private Map<String, Integer> parseMapping(String mappingStr) {
        Map<String, Integer> map = new HashMap<>();
        String[] pairs = mappingStr.split(",");
        for (String pair : pairs) {
            String[] kv = pair.split("=");
            if (kv.length == 2) {
                String key = kv[1].trim();
                Integer value = Integer.parseInt(kv[0].trim());
                map.put(key, value);
            }
        }
        return map;
    }

    private Map<Integer, String> parseReverseMapping(String mappingStr) {
        Map<Integer, String> map = new HashMap<>();
        String[] pairs = mappingStr.split(",");
        for (String pair : pairs) {
            String[] kv = pair.split("=");
            if (kv.length == 2) {
                Integer key = Integer.parseInt(kv[0].trim());
                String value = kv[1].trim();
                map.put(key, value);
            }
        }
        return map;
    }
}
